#include "controllableObject.h"

ControllableObject::ControllableObject(void)
{
	this->init();
}
ControllableObject::ControllableObject(std::vector<sf::Key::Code> &inputVector)
{
	this->init();
	this->InputKeys(inputVector);

}
void ControllableObject::init()
{	
	//set all the input functions to the empty function
	//let the user change this as needed
	for(int i = 0; i < NUM_INPUT_KEYS * 2; i++)
	{
		inputFunctions[i] = &voidFunction;
	}
	for(int i = 0; i < NUM_INPUT_KEYS; i++)
	{
		this->keysPressed[i] = false;
	}
	this->initKeys();
}
void ControllableObject::initKeys()
{
	defaultKeys[0] = sf::Key::S;
	defaultKeys[1] = sf::Key::X;
	defaultKeys[2] = sf::Key::C;
	defaultKeys[3] = sf::Key::D;
	defaultKeys[4] = sf::Key::W;
	defaultKeys[5] = sf::Key::E;
	defaultKeys[6] = sf::Key::Q;
	defaultKeys[7] = sf::Key::R;
	defaultKeys[8] = sf::Key::Return;
	defaultKeys[9] = sf::Key::RShift;
	defaultKeys[10] = sf::Key::Up;
	defaultKeys[11] = sf::Key::Down;
	defaultKeys[12] = sf::Key::Left;
	defaultKeys[13] = sf::Key::Right;
	defaultKeys[14] = sf::Key::J;
	defaultKeys[15] = sf::Key::M;
	defaultKeys[16] = sf::Key::N;
	defaultKeys[17] = sf::Key::H;

	for(int i = 0; i < NUM_INPUT_KEYS; i++)
	{
		this->keys[i] = defaultKeys[i];
	}
}
void ControllableObject::InputKeys(std::vector<sf :: Key :: Code> &inputVector)
{
	for(int i = 0; i < NUM_INPUT_KEYS; i++)
	{
		if(inputVector.size() > (unsigned)i && inputVector.at(i) != NULL)
		{
			this->keys[i] = inputVector.at(i);
		}
	}
}
//these are public
void ControllableObject::Input(const sf::Input &input)
{
	this->Input(input, this, 0);
}
void ControllableObject::Input(const sf::Input &input, void *extraData)
{
	this->Input(input, this, extraData);
}
//this function is protected
void ControllableObject::Input(const sf::Input &input, void *object, void *extraData)
{
	for(int i = 0; i < NUM_INPUT_KEYS; i++ )
	{
		//This will run the button if it is pressed down
		if(input.IsKeyDown(keys[i]))
		{
			this->inputFunctions[i](object, extraData);
		}
		//This will run the button if it is not pressed down
		if(!input.IsKeyDown(keys[i]))
		{
			//we add the number of keys to i so that we get passed
			//the key down functions and then the key[0]'s up
			//state is function[0+NUM_INPUT_KEYS] = function[NUM_INPUT_KEYS] one passed
			//the last function for the key down state.
			this->inputFunctions[i+NUM_INPUT_KEYS](object, extraData);
		}
	}
}

void voidFunction(void *object, void* extraData)
{
	//Does nothing but allows for the func member variable
	//to point at something if the input for that function
	//is not need for the object.
}